package com.yf.domain.security;

import com.alibaba.fastjson2.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author chentianwei
 * @date 2023/4/17 13:44
 * @description department domain
 */

@Getter
@Setter
@TableName(value = "sys_dept")
public class Department implements Serializable {

    @TableId(value = "dept_id", type = IdType.AUTO)
    @Schema(name = "Id", hidden = true)
    private Long id;

    @TableField(exist = false)
    @JSONField(serialize = false)
    @Schema(name = "角色关联信息")
    private Set<Role> roles;

    @TableField(exist = false)
    private List<Department> children;

    @TableField(value = "dept_sort")
    private Integer deptSort;

    @TableField(value = "name")
    private String name;

    @TableField(value = "enabled")
    @Schema(name = "是否启用")
    private Boolean enabled;

    @TableField(value = "pid")
    @Schema(name = "子id")
    private Long pid;

    @TableField(value = "sub_count")
    @Schema(name = "子节点数量")
    private Integer subCount;

    @TableField(value = "create_time",insertStrategy = FieldStrategy.DEFAULT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private Timestamp createTime;

    @TableField(value = "update_time",updateStrategy = FieldStrategy.ALWAYS)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
    private Timestamp updateTime;

    @TableField(value = "create_by",insertStrategy = FieldStrategy.DEFAULT)
    private String createBy;

    @TableField(value = "update_by",updateStrategy = FieldStrategy.ALWAYS)
    private String updateBy;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        Department dept = (Department) o;
        return Objects.equals(id, dept.id) &&
                Objects.equals(name, dept.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }

    public Boolean getHasChildren() {
        return subCount > 0;
    }

    public Boolean getLeaf() {
        return subCount <= 0;
    }

    public String getLabel() {
        return name;
    }
}
